#include <string.h>
#include <stdio.h>
#include "dev_info_cfg.h"
#include "nvs_dev_info/nvs_dev_info.h"
#include "utils_log.h"

int GetDevInfoCfg(DevInfo_t *info)
{
    if (NULL == info){
        printf("[%s-%d]:info is invalid\r\n",__func__, __LINE__);
        LOG_TO_FILE("info is invalid");
        return -1;
    }

    memset(info, 0, sizeof(DevInfo_t));
    GetNvsDevType(&info->devType, sizeof(info->devType));
    GetNvsProductID(info->productID, sizeof(info->productID));
    GetNvsDevID(info->devID, sizeof(info->devID));
    GetNvsAirConGateInfo(&info->gateInfo, sizeof(info->gateInfo));
    GetNvsMeterInfo(&info->meter, sizeof(info->meter));

    printf("Dev info devType:%d, devID:%s, productID:%s, airConGateAddr:%d\n", 
            info->devType, info->devID, info->productID, info->gateInfo.airConGateAddr);

    for(int i = 0; i<MAX_SUB_DEV_ARRAY_LEN; i++){
        if(strlen(info->gateInfo.subDevice[i].subDevID) > 0){
            printf("sub device:No.%d, ID:%s\r\n", info->gateInfo.subDevice[i].number, info->gateInfo.subDevice[i].subDevID);
        }
    }
    return 0;
}

int SetDevInfoCfg(DevInfo_t *info)
{
    if (NULL == info){
        printf("[%s-%d]:info is invalid\r\n",__func__, __LINE__);
        LOG_TO_FILE("info is invalid");
        return -1;
    }
    
    int ret = SetNvsDevType(&info->devType, sizeof(info->devType));
    ret |= SetNvsDevID(info->devID, strlen(info->devID));
    ret |= SetNvsProductID(info->productID, strlen(info->productID));
    ret |= SetNvsAirConGateInfo(&info->gateInfo, sizeof(info->gateInfo));
    ret |= SetNvsMeterInfo(&info->meter, sizeof(info->meter));

    return ret;
}